package com.peanut.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peanut.common.Result;
import com.peanut.domain.bo.TabCouponBO;
import com.peanut.domain.bo.TabUserCouponBO;
import com.peanut.domain.po.TabCouponPO;
import com.peanut.dto.TabCouponDTO;
import com.peanut.dto.TabUserCouponDTO;
import com.peanut.service.TabCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("coupon")
@Api("优惠券管理Api")
public class CouponController {

    @Resource
    private TabCouponService couponService;

    /**
     * 添加优惠券
     * @param dto : 优惠券实体类
     * @return
     */
    @PostMapping
    @ApiOperation("添加优惠券")
    @ApiImplicitParam(name = "dto",value = "优惠券实体类",required = true,paramType = "body")
    public Result addCoupon(@RequestBody TabCouponDTO dto){
        try {
            TabCouponBO bo = new TabCouponBO();
            BeanUtils.copyProperties(dto,bo);
            int i = couponService.addCoupon(bo);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 查询所有优惠券,并分页
     * @param pageNumber : 当前页面
     * @param pageSize : 每页条数
     * @return
     */
    @PostMapping("findAll")
    @ApiOperation("查询所有优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber",value = "当前页面",required = true,paramType ="query"),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,paramType ="query")
    })
    public Result findAll(@RequestParam(value = "pageNumber",defaultValue = "1")Integer pageNumber,
                          @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        try {
            Page<TabCouponPO> page = couponService.findAll(pageNumber,pageSize);
            return Result.OK().setData(page);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 修改优惠券
     * @param dto : 优惠券实体类
     * @return
     */
    @PutMapping
    @ApiOperation("修改优惠券")
    @ApiImplicitParam(name = "dto",value = "优惠券实体类",required = true,paramType = "body")
    public Result updateCoupon(@RequestBody TabCouponDTO dto){
        try {
            TabCouponBO bo = new TabCouponBO();
            BeanUtils.copyProperties(dto,bo);
            int i = couponService.updateCoupon(bo);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 根据ID删除优惠券
     * @param couponId : 优惠券ID
     * @return
     */
    @DeleteMapping("/{couponId}")
    @ApiOperation("根据ID删除优惠券")
    @ApiImplicitParam(name = "infoId",value = "优惠券ID",required = true,paramType ="path")
    public Result deleteById(@PathVariable Long couponId){
        try {
            int i = couponService.deleteById(couponId);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     *对应用户的优惠券管理
     * @param userCouponDTO : 优惠券中间表实体类
     * @return
     */
    @PostMapping("getUserIdFindCoupon")
    @ApiOperation("对应用户的优惠券管理")
    @ApiImplicitParam(name = "userCouponDTO",value = "优惠券中间表实体类",required = true,paramType = "body")
    public Result getUserIdFindCoup(@RequestBody TabUserCouponDTO userCouponDTO){
        try {
            TabCouponBO bo = new TabCouponBO();
            bo.setCouponName(userCouponDTO.getCouponName());
            bo.setCouponPrice(userCouponDTO.getCouponPrice());
            TabUserCouponBO userCouponBO = new TabUserCouponBO();
            BeanUtils.copyProperties(userCouponDTO,userCouponBO);
            int i = couponService.getUserIdFindCoup(userCouponBO,bo);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 根据ID删除优惠券和优惠券中间表
     * @param couponId : 优惠券ID
     * @return
     */
    @DeleteMapping("/deleteByUserCouponAndCoupon/{couponId}")
    @ApiOperation("根据ID删除优惠券和优惠券中间表")
    @ApiImplicitParam(name = "couponId",value = "优惠券ID",required = true,paramType ="path")
    public Result deleteByUserCouponAndCoupon(@PathVariable Long couponId){
        try {
            int i = couponService.deleteByUserCouponAndCoupon(couponId);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }
}
